$(function () {

   // 渲染饼图
   echartHandler()
   function echartHandler() {
      getData().then((data) => {
         setEcharts(data)
      })
   }
   // 获取数据
   function getData() {
      return new Promise((resolve) => {
         $.get('/admin/admindata', res => {
            const { msg, data } = res
            if (msg === 'ok') return resolve(data)
            resolve([])
         })
      })
   }
   // 设置 echarts 配置项
   function setEcharts(arr) {
      const myecharts = echarts.init($('#myechart')[0])
      const names = []
      const values = []
      arr.forEach(item => {
         names.push(item.name)
         values.push(item.value)
      })
      const option = {
         title: {
            text: '店铺数据统计条形图'
         },
         tooltip: {
            trigger: 'axis',
            axisPointer: {
               type: 'shadow'
            }
         },
         legend: {},
         grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
         },
         xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01]
         },
         yAxis: {
            type: 'category',
            data: names
         },
         series: [
            {
               type: 'bar',
               data: values,
               barMaxWidth:'60%',
               itemStyle: {
                  normal: {
                     color: function (params) {
                        var colorlist = ['#DA251D', '#E67716', '#FBC300', '#11440f', '#32585a', '#30B1C9'];
                        return colorlist[params.dataIndex];
                     }
                  }
               }
            }
         ]
      }
      option && myecharts.setOption(option)
   }
})